﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Data.Filtering;
using Model;
using BLL;
using DevExpress.Xpo;
using POS.Framework;
using DevExpress.Utils;

namespace POS.ModForm.BaseInfo
{
    public partial class frmProvider : DevExpress.XtraEditors.XtraForm
    {
        public frmProvider()
        {
            InitializeComponent();
        }
        private void frmProvider_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this.gridView2.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                SetButtonClickEvent();
                SearchData();
            }
        }
        void SetButtonClickEvent()
        {
            this.listButtonUC.IsHide(new List<string> { "btnNew" });
            this.listButtonUC.Query.Click += new EventHandler(Query_Click);
        }
        void Query_Click(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                SearchData();
                wdf.Close();
            }
        }
        void SearchData()
        {
            DevExpress.Data.Filtering.GroupOperator goFilter = new DevExpress.Data.Filtering.GroupOperator();
            if (!string.IsNullOrEmpty(this._CreateDT.Text))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT>='" + this._CreateDT.Text + "'"));
            if (!string.IsNullOrEmpty(this._CreateDTEnd.Text))
                goFilter.Operands.Add(CriteriaOperator.Parse("CreateDT<='" + this._CreateDTEnd.Text + " 23:58:58'"));
            if (!string.IsNullOrWhiteSpace(this._ProviderSearch.Text))
            {
                switch (this._ProviderID.SelectedIndex)
                {
                    case 0:
                        goFilter.Operands.Add(CriteriaOperator.Parse("ProviderCode like '" + this._ProviderSearch.Text + "%'"));
                        break;
                    case 1:
                        goFilter.Operands.Add(CriteriaOperator.Parse("ProviderName like '" + this._ProviderSearch.Text + "%'"));
                        break;
                    case 2:
                        goFilter.Operands.Add(CriteriaOperator.Parse("Address like '" + this._ProviderSearch.Text + "%'"));
                        break;
                }
            }
            XPCollection<B_Provider> item = new XPCollection<B_Provider>(DatabaseSchema.Session, goFilter);
            this._gcStockDetail.DataSource = item;
            SpecialGridView.SetGridViewIndicatorWidth(item.Count, this.gridView2);
        }
    }
}